package com.daqsoft.service.project;

import com.daqsoft.dao.project.AttachmentDao;
import com.daqsoft.data.project.Attachment;
import com.daqsoft.data.project.Project;
import com.daqsoft.data.project.Task;
import com.daqsoft.dto.GeneralResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件管理服务层
 *
 * @author 周麟东 on 10:48
 */
@Service
public class AttachmentService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AttachmentDao dao;

    @Autowired
    private TaskService taskService;

    @Autowired
    private ProjectService projectService;

    public List<Attachment> getAttByTaskId(int taskId) {
        Task task = taskService.get(taskId);
        if (task == null) {
            return new ArrayList<>();
        }
        return dao.getAttachmentByTask(task);
    }

    public Attachment save(Task task, Attachment attachment) {
        attachment.setTask(task);
        attachment.setProject(task.getProject());
        attachment.setProjectFile(false);
        return dao.save(attachment);
    }

    public Attachment save(Project project, Attachment attachment) {
        attachment.setProjectFile(true);
        attachment.setProject(project);
        return dao.save(attachment);
    }

    public Attachment get(int attId) {
        return dao.findOne(attId);
    }

    public void remove(int attId) {
        dao.delete(attId);
    }

    public Attachment save(Attachment attachment) {
        return dao.save(attachment);
    }

    /**
     * 拷贝附件
     *
     * @param attachments 想要拷贝的附件列表
     * @param newTask     要拷贝到哪个节点
     * @return 拷贝完成的附件列表
     */
    @Transactional
    public List<Attachment> copyAttachments(List<Attachment> attachments, Task newTask) {
        List<Attachment> newAttachments = new ArrayList<>();
        for (Attachment att : attachments) {
            Attachment newAtt = cloneAttachment(att);
            newAtt.setTask(newTask);
            newAtt = dao.save(newAtt);
            newAttachments.add(newAtt);
        }
        return newAttachments;
    }

    /**
     * 克隆附件对象 但是不是单纯的克隆 以下值将发生变化
     * id设置为0
     * task设置为空
     *
     * @param att 被克隆的对象
     * @return 新的克隆对象
     */
    private Attachment cloneAttachment(Attachment att) {
        Attachment newAtt = new Attachment();
        newAtt.setPath(att.getPath());
        newAtt.setDesc(att.getDesc());
        newAtt.setFileName(att.getFileName());
        newAtt.setUrl(att.getUrl());
        return newAtt;
    }

    /**
     * 通过项目ID获取附件
     *
     * @param projectId 项目ID
     * @return 附件列表
     */
    public List<Attachment> getAttByProjectId(Integer projectId) {
        Project project = projectService.get(projectId);
        if (project == null) {
            return null;
        }
        return dao.getByProjectAndProjectFile(project , true);
    }

    /**
     * 删除文件
     *
     * @param path 文件地址
     * @return 是否删除成功
     */
    public boolean deleteFile(String path) {
        File file = new File(path);
        return file.delete();
    }

    /**
     * 删除
     * @param attachment 删除对象
     */
    @Transactional
    public boolean delete(Attachment attachment) {
        //先删除对象
        dao.delete(attachment);
        boolean flag = deleteFile(attachment.getPath());
        if (flag){
            logger.info("删除{}文件成功!",attachment.getFileName());
        }
        return flag;
    }

    /**
     * 按照ID删除
     * @param id id
     */
    public void delete(int id) {
        Attachment attachment = get(id);
        delete(attachment);
    }
}
